//
//  ImageScale.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/17.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "ImageScaleAnimator.h"
#import "ImageDetailController.h"


@implementation ImageScaleAnimator
+(instancetype)animator{
    ImageScaleAnimator * animator = [[[self class] alloc] _init];
    return animator;
}

-(id)_init{
    if(self = [super init]){
        self.toDuration = 0.3;
        self.backDuration = 0.3;
    }
    return self;
}


-(void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    id fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIImageView * orginImageView = [fromVC imageView];
    ImageDetailController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    UIImageView * imgv= toVC.imageView;
    imgv.width = orginImageView.width;
    imgv.height = orginImageView.height;
    imgv.x = orginImageView.x;
    imgv.y = orginImageView.y;
    
    [UIView animateWithDuration:self.toDuration animations:^{
        imgv.x = 0;
        imgv.y = (ScreenHeight - ScreenWidth)/2;
        imgv.width = ScreenWidth;
        imgv.height = ScreenWidth;
    }completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];

}

-(void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    id fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    id toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView insertSubview:[toVC view] atIndex:0];
   
//    UIImageView * orgingImageView = [toVC imageView];
    UIImageView * fromImageView = [fromVC imageView];
//    CGFloat toX = orgingImageView.x;
//    CGFloat toY = orgingImageView.y;
//    CGFloat toW = orgingImageView.width;
//    CGFloat toH = orgingImageView.height;
    [UIView animateWithDuration:self.backDuration animations:^{
        fromImageView.x = 0;
        fromImageView.y = 0;
//        fromImageView.width = toW;
//        fromImageView.height = toH;
    }completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
   
}

@end
